"use strict";

const Service = require("egg").Service;
const errorLog = require("../utils/errorLog");
const { col } = require("sequelize");

class UserService extends Service {
  async login(account, password) {
    try {
      const user = await this.app.model.UsersModel.findOne({
        where: { account, password, isShow: 1 },
        include: [
          {
            model: this.app.model.RoleModel,
            as: "role",
            attributes: ["roleName", "roleRoute", [col("id"), "roleId"]],
          },
        ],
      });
      delete user.dataValues.password;
      return user;
    } catch (err) {
      errorLog("UserService----->login", err);
      return false;
    }
  }

  async createUser(userObj) {
    try {
      userObj.createAt = Date.now();
      userObj.updateAt = Date.now();
      userObj.isShow = 1;
      const isOne = await this.app.model.UsersModel.findOne({
        where: { account: userObj.account },
      });
      if (isOne) return { code: 400, msg: "该账号已存在" };
      const user = await this.app.model.UsersModel.create(userObj);
      return user;
    } catch (err) {
      errorLog("UserService----->createUser", err);
      return false;
    }
  }

  async updateUser(id, userObj) {
    try {
      if (!id || id < 0 || id.length <= 0) {
        return { code: 500, msg: "没有用户id，修改失败" };
      }

      userObj.updateAt = Date.now();
      if (userObj.account) delete userObj.account;
      if (userObj.name.length === 0) delete userObj.name;
      if (userObj.password.length === 0) {
        delete userObj.password;
      } else if (userObj.password.length < 6 || userObj.password.length > 20) {
        return { code: 400, msg: "密码长度在6-20位之间" };
      }
      if (userObj.roleId == 0) delete userObj.roleId;
      if (userObj.address.length === 0) delete userObj.address;
      if (userObj.phone.length === 0) delete userObj.phone;
      if (userObj.gender.length === 0) delete userObj.gender;
      if (!userObj.age || userObj.age <= 0 || userObj.age.length == 0) {
        delete userObj.age;
      }

      const user = await this.app.model.UsersModel.update(userObj, {
        where: { id },
      });
      return user;
    } catch (err) {
      errorLog("UserService----->update", err);
      return false;
    }
  }

  async updateUserPasswordById(userObj) {
    try {
      const { id, password, newPassword } = userObj;
      const user = await this.app.model.UsersModel.findOne({ where: { id } });
      if (user) {
        if (user.dataValues.password === password) {
          const user = await this.app.model.UsersModel.update(
            { password: newPassword },
            { where: { id } }
          );
          return user;
        }
        return { code: 400, msg: "旧密码错误" };
      }
      return false;
    } catch (err) {
      errorLog("UserService----->updateUserPasswordById", err);
      return false;
    }
  }

  async deleteUser(id) {
    try {
      if (id == 1) return false;
      const user = await this.app.model.UsersModel.update(
        { isShow: 0, roleId: 2 },
        { where: { id } }
      );
      return user;
    } catch (err) {
      errorLog("UserService----->deleteUser", err);
      return false;
    }
  }

  async getUserList(currentPage, pageSize) {
    console.log("currentPage", currentPage, "pageSize", pageSize);
    try {
      const userList = await this.app.model.UsersModel.findAll({
        where: { isShow: 1 },
        offset: (currentPage - 1) * parseInt(pageSize),
        limit: parseInt(pageSize),
        include: [
          {
            model: this.app.model.RoleModel,
            as: "role",
            attributes: ["roleName"],
          },
        ],
      });
      return userList;
    } catch (err) {
      errorLog("UserService----->getUserList", err);
      return false;
    }
  }
}

module.exports = UserService;
